/**
 * Created by DevilsEyes on 2016/5/3.
 */
'use strict';

var $file = require('../db/file.js');

let v = (v, def)=>v === undefined ? def : v;
let overTime = 1000 * 60 * 60;

var fileType = {
    png: 'image/png',
    jpg: 'image/jpeg',
    default: 'application/octet-stream'
};

module.exports = {

    save: (map)=> {
        let file_Entity = new $file({
            name: v(map.name, ''),
            type: v(map.type, fileType.default),
            path: map.path,
            use: false,
            size: map.size,
            time: new Date().getTime() + overTime
        });

        return new Promise((O, X)=> {
            file_Entity.save((err, file)=> {
                O(file);
            })
        })
    },

    getFileByPath: (path)=> {
        return new Promise((O, X)=> {
            $file
                .find({
                    path: path
                })
                .then((files)=> {
                    if (files[0]) {
                        O(files[0]);
                    } else {
                        X({
                            code: 9999,
                            msg: '没有此文件'
                        })
                    }
                })
        })
    },

    updatedByPath: (path, map)=> {
        let data = {
            name: map.name || ''
        };
        if (map.use !== undefined)data.use = !!map.use;

        return new Promise((O, X)=> {
            $file
                .where({path: path})
                .update(map)
                .then((res)=> {
                    if (res.ok == 1) {
                        O();
                    } else {
                        X({
                            code: 9999,
                            msg: '未知错误'
                        })
                    }
                })
        })
    },

    deleteByPath: (path)=> {
        return new Promise((O, X)=> {
            $file
                .where({path: path})
                .update({use: false})
                .then((res)=> {
                    if (res.ok == 1) {
                        O();
                    } else {
                        X({
                            code: 9999,
                            msg: '未知错误'
                        })
                    }
                })
        })
    },

    getUnusedFile: ()=> {
        return new Promise((O, X)=> {
            $file
                .where({use: false})
                .where('time')
                .lte(new Date().getTime())
                .then((files)=> {
                    if (files) {
                        O(files);
                    } else {
                        X({
                            code: 9999,
                            msg: '未知错误'
                        })
                    }
                })
        })
    },
    removeByPath: (path)=> {
        return new Promise((O, X)=> {
            $file
                .remove({
                    path: path
                })
                .then(()=> {
                    O()
                })
        })
    },

    //危险方法
    clear: ()=> {
        return new Promise((O, X)=> {
            $file
                .remove({})
                .then(()=> {
                    O()
                })
        })
    },
    getAll: () => {
        return new Promise((O, X)=> {
            $file
                .find({})
                .then((files)=> {
                    O(files);
                })
        })
    }

};